package com.brillilab.service.core.global.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.method.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.dao.mapper.global.GlobalKitMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.po.global.GlobalKit;
import com.brillilab.domain.po.global.GlobalReagent;
import com.brillilab.domain.po.global.GlobalReagentAntibody;
import com.brillilab.domain.po.global.GlobalReagentPlasmid;
import com.brillilab.domain.po.global.GlobalReagentPrimer;
import com.brillilab.domain.vo.global.GlobalKitInfoVo;
import com.brillilab.domain.vo.global.GlobalReagentInfoVo;
import com.brillilab.service.constant.ServiceConstant;
import com.brillilab.service.core.global.IGlobalKitService;
import com.brillilab.service.core.global.IGlobalReagentService;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service

public class GlobalKitServiceImpl implements IGlobalKitService {
    @Resource
    private GlobalKitMapper globalKitMapper;
    @Resource
    private IGlobalReagentService globalReagentService;

    @Override
    public GlobalKit selectById(Long id) {
        return globalKitMapper.selectById(id);
    }

    @Override
    public List<GlobalKit> selectList(Integer classify, String itemNo) {
        LambdaQueryWrapper<GlobalKit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GlobalKit::getClassify, classify);
        wrapper.eq(GlobalKit::getItemNo, itemNo);
        wrapper.eq(GlobalKit::getIsDelete, IsDeleteEnum.NO.getValue());
        return globalKitMapper.selectList(wrapper);
    }


    @Override
    public GlobalKitInfoVo getGlobalKitInfo(Long kitId) {
        GlobalKit globalKit = globalKitMapper.selectById(kitId);
        //组装kit信息
        GlobalKitInfoVo infoVo = MyBeanUtils.copyBean(globalKit, GlobalKitInfoVo.class);
        infoVo.setFromId(globalKit.getId());
//		infoVo.setMultiple(1);
        List<GlobalReagentInfoVo> reagentInfoList = new ArrayList<>();
        List<GlobalReagent> reagentList = globalReagentService.selectList(kitId);
        for (GlobalReagent reagent : reagentList) {
            if (globalKit.getKitType() == 0) {//单成分
//				infoVo.setMultiple(reagent.getMultiple());
                infoVo.setConcentration(reagent.getConcentration());
                infoVo.setNumeratorUnit(reagent.getNumeratorUnit());
                infoVo.setDenominatorUnit(reagent.getDenominatorUnit());
            }
            //组装reagent信息和属性信息
            GlobalReagentInfoVo reagentInfoVo = MyBeanUtils.copyBean(reagent, GlobalReagentInfoVo.class);
            if (reagent.getReagentTypeId().equals(ServiceConstant.antibody_reagent_type_id)) {
                GlobalReagentAntibody globalAntibody = globalReagentService.selectAntibody(reagent.getKitId(), reagent.getId());
                reagentInfoVo.setReagentAntibody(globalAntibody);
            } else if (reagent.getReagentTypeId().equals(ServiceConstant.primer_reagent_type_id)) {
                GlobalReagentPrimer globalPrimer = globalReagentService.selectPrimer(reagent.getKitId(), reagent.getId());
                reagentInfoVo.setReagentPrimer(globalPrimer);
            } else if (reagent.getReagentTypeId().equals(ServiceConstant.plasmid_reagent_type_id)) {
                GlobalReagentPlasmid globalPlasmid = globalReagentService.selectPlasmid(reagent.getKitId(), reagent.getId());
                reagentInfoVo.setReagentPlasmid(globalPlasmid);
            }
            reagentInfoList.add(reagentInfoVo);
        }
        infoVo.setGlobalReagentList(reagentInfoList);
        return infoVo;
    }

    @Override
    public PageVo<GlobalKit> selectKitList(Boolean hasAttach, Boolean hasRelateMethod, Long companyId, String reagentNamePrefix, Long reagentTypeId, Boolean canConvert,int pageNum, int pageSize) {
        IPage<GlobalKit> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<GlobalKit> query = new LambdaQueryWrapper<>();
        if (hasAttach != null && hasAttach) {
            query.isNotNull(GlobalKit::getInstruction);
        } else {
            query.isNull(GlobalKit::getInstruction);
        }
        if (hasRelateMethod != null) {
            if(hasRelateMethod) {
                query.isNotNull(GlobalKit::getRelateMethodId);
            }else{
                query.isNull(GlobalKit::getRelateMethodId);
            }
        }
        if(canConvert!=null){
            if(canConvert){
                query.eq(GlobalKit::getHasSplitSign, BoolEnum.TRUE.getValue());
            }else{
                query.eq(GlobalKit::getHasSplitSign, BoolEnum.FALSE.getValue());
            }
        }
        query.eq(GlobalKit::getReagentTypeId, reagentTypeId);
        query.eq(GlobalKit::getCompanyId, companyId);
        if(!StringUtils.isEmpty(reagentNamePrefix)) {
            query.likeRight(GlobalKit::getName, reagentNamePrefix);
        }
        IPage<GlobalKit> pageResult = globalKitMapper.selectPage(page, query);
        return new PageVo<GlobalKit>(pageResult);
    }

    @Override
    public Integer getKitRelateMethodStatistics() {
        LambdaQueryWrapper<GlobalKit> query = new LambdaQueryWrapper<>();
        query.isNotNull(GlobalKit::getRelateMethodId);
        return globalKitMapper.selectCount(query);
    }

    @Override
    public List<GlobalKit> searchCompanyByReagentType(Long reagentTypeId) {
        return globalKitMapper.searchCompanyByReagentType(reagentTypeId);
    }

    @Override
    public List<Long> selectReagentTypeIdList() {
        return globalKitMapper.selectReagentTypeIdList();
    }

    @Override
    public boolean updateById(GlobalKit globalKit) {
        Assert.notNull(globalKit.getId(), ResultEnum.DATA_NOT_EXIST.getMessage());
        return globalKitMapper.updateById(globalKit)>0;
    }

    @Override
    public boolean batchRelateMethod(List<Long> globalKitId, Long publicMethodId) {
        return globalKitMapper.batchRelateMethod(globalKitId,publicMethodId)>0;
    }

    @Override
    public GlobalKit selectByRelateMethodId(Long methodId) {
        return globalKitMapper.selectOne(
                new LambdaQueryWrapper<GlobalKit>()
                        .eq(GlobalKit::getRelateMethodId,methodId)
                        .eq(GlobalKit::getIsDelete,BoolEnum.FALSE.getValue()));
    }

    @Override
    public List<GlobalKit> selectTopNoMethodKit() {
        return globalKitMapper.selectTopNoMethodKit();
    }

    @Override
    public int updateRelateInfo(List<GlobalKit> kits) {
        if (!CollectionUtils.isEmpty(kits)){
            return globalKitMapper.updateRelateInfo(kits);
        }
        return 0;
    }
}
